import { Game, GameComponent } from "@sapi-game/main";
import { Duration } from "@sapi-game/utils";
import { eleCatchState } from "../state";
import { Timer } from "@sapi-game/gameComponent/common/timer/timer";
import { DisplaySlotId } from "@minecraft/server";

export class EleCatchStateDaemon extends GameComponent<eleCatchState> {
    override onAttach(): void {
        this.subscribe(
            Game.events.interval,
            () => {
                this.context.group.getAllPlayers().forEach((p) => {
                    if (p.locateCooldown > 0) p.locateCooldown--;
                    if (p.attckCooldown > 0) p.attckCooldown--;
                });
                //清理下线过久玩家
                this.context.group.forEachGroup((g) => {
                    g.removeWhere((p) => !p.isValid && p.ttl == 0);
                });
            },
            new Duration(20)
        );
        const timer = this.state.getComponent(Timer);
        this.subscribe(timer.events.tick, (t) => {
            this.state.showDb.clear();
            this.state.showDb.setDisplaySlot(DisplaySlotId.Sidebar);
            this.state.showDb.set("剩余时间:§c§l" + t.remainingTime, 1);
        });
    }
}
